package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.mapper.BasicWarehouseMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import  cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * 仓库表（原系统：库房表） 服务实现类
 */
@Service
public class BasicWarehouseServiceImpl extends ServiceImpl<BasicWarehouseMapper, BasicWarehouse> implements BasicWarehouseService {
    
    @Resource
    private DeptService deptService;

    @Override
    public void add(BasicWarehouseParam param){
        BasicWarehouse entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicWarehouseParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicWarehouseParam param){
        BasicWarehouse oldEntity = getOldEntity(param);
        BasicWarehouse newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicWarehouseResult findBySpec(BasicWarehouseParam param,Long deptId,Long operateId){
        return this.baseMapper.findBySpec(param,deptId, operateId);
    }

    @Override
    public List<BasicWarehouseResult> findListBySpec(BasicWarehouseParam param,Long deptId,Long operateId){
        return this.baseMapper.findListBySpec(param,deptId,operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicWarehouseParam param,String condition,Long deptId,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition,deptId, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<BasicWarehouseResult> findStoragelistPageBySpec(BasicWarehouseParam param,String condition,Long deptId){
        return this.baseMapper.findStoragelistPageBySpec(param,condition,deptId);
    }

    @Override
    public List<Map<String,String>> getRecommendWarehouselist(Long warehouseId, String goodsNumber) {
        return this.baseMapper.getRecommendWarehouselist(warehouseId, goodsNumber);
    }

    @Override
    public LayuiPageInfo getWarehouseByStoreId(String storeId, Long deptId, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getWarehouseByStoreId(pageContext,storeId, deptId, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicWarehouseParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicWarehouse getOldEntity(BasicWarehouseParam param) {
        return this.getById(getKey(param));
    }

    private BasicWarehouse getEntity(BasicWarehouseParam param) {
        BasicWarehouse entity = new BasicWarehouse();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<BasicWarehouse> listByDeptId(Long deptId) {
		return this.baseMapper.listByDeptId(deptId);
	}

	@Override
	public List<BasicWarehouseResult> findAvailableList(Long deptId, Long deliverId,Long operateId) {
		BasicWarehouseParam param = new BasicWarehouseParam();
		param.setDeliverId(deliverId);
        return this.baseMapper.findListBySpec(param,deptId,operateId);
	}

}
